package com.diaohw.platform.framework.db.flex.listener;

import com.diaohw.platform.common.obj.thread.LoginThread;
import com.diaohw.platform.framework.db.flex.obj.model.BaseLogModel;
import com.diaohw.platform.framework.db.flex.obj.model.BaseModel;
import com.mybatisflex.annotation.InsertListener;

import java.time.LocalDateTime;

public class GlobalInsertListener implements InsertListener {
    @Override
    public void onInsert(Object entity) {
        if (entity instanceof BaseModel base) {
            base.setUpdatedTime(LocalDateTime.now());
            base.setCreatedTime(LocalDateTime.now());
            if (LoginThread.isLogin()) {
                base.setUpdatedBy(LoginThread.getLoginId());
                base.setCreatedBy(LoginThread.getLoginId());
            }
        }

        if (entity instanceof BaseLogModel log) {
            log.setCreatedTime(LocalDateTime.now());
        }
    }
}
